//
//  LBCategorySearchBar.m
//  AFNetworking
//
//  Created by luckyRoy on 2018/2/1.
//

#import "LBCategorySearchBar.h"
#import "Masonry.h"

@interface LBCategorySearchBar ()

@property (nonatomic, strong) UIView *searchContainer;

@end

@implementation LBCategorySearchBar

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

- (UIView *)searchContainer
{
    if (!_searchContainer)
    {
        _searchContainer = [[UIView alloc] initWithFrame:self.bounds];
        _searchContainer.layer.cornerRadius = 2;
        _searchContainer.backgroundColor = [UIColor whiteColor];
        _searchContainer.alpha = 0.3;
    }
    
    return _searchContainer;
}

- (UIButton *)cancelBtn
{
    if (!_cancelBtn)
    {
        _cancelBtn = [[UIButton alloc] init];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _cancelBtn;
}

- (UISearchBar *)searchBar
{
    if (!_searchBar)
    {
        _searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
        [_searchBar setBackgroundImage:nil];
        
        // 光标颜色
        _searchBar.tintColor = [UIColor orangeColor];
        
        // 清除背景色
        _searchBar.barTintColor = [UIColor clearColor];
        _searchBar.backgroundColor = [UIColor clearColor];
        
        _searchBar.showsCancelButton = NO;
        
        // 设置输入框
        UITextField *searchTextField = searchTextField = [_searchBar valueForKey:@"_searchField"];
        searchTextField.backgroundColor = [UIColor clearColor];
        searchTextField.font = [UIFont systemFontOfSize:14];
        searchTextField.textColor = [UIColor whiteColor];
        
        UILabel *sbPlaceholderLabel = [searchTextField valueForKey:@"placeholderLabel"];
        sbPlaceholderLabel.textColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.5];//改变颜色
        
        // 去掉背景 否则ios11 显示阴影
        for (UIView *subView in _searchBar.subviews) {
            if ([subView isKindOfClass:[UIView  class]] && subView.subviews.count > 0) {
                [[subView.subviews objectAtIndex:0] removeFromSuperview];
            }
        }
        
        //
        ;
        [_searchBar setImage:[UIImage imageNamed:@"Category.bundle/lb_resource_icon_search"]
            forSearchBarIcon:UISearchBarIconSearch
                       state:UIControlStateNormal];
        
        [_searchBar setImage:[UIImage imageNamed:@"Category.bundle/lb_res_search_close"]
            forSearchBarIcon:UISearchBarIconClear
                       state:UIControlStateNormal];
    }
    
    return _searchBar;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor clearColor];
        self.clipsToBounds = YES;
        
        [self initSubView];
    }
    
    return self;
}

- (void)initSubView
{
    
    [self addSubview:self.searchContainer];
    
    [self addSubview:self.searchBar];
    
    [self addSubview:self.cancelBtn];
    
    // layout ui
    [self layoutUI];
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //
}

- (void)layoutUI
{
    CGFloat leftMargin = 10;
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.offset(-leftMargin);
        make.centerY.offset(0);
        make.width.mas_lessThanOrEqualTo(60);
    }];
    
    [self.searchContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.cancelBtn.mas_left).offset(-leftMargin/2);
        make.top.bottom.equalTo(self);
        make.left.offset(leftMargin);
        
        make.height.mas_equalTo(30);
    }];
    
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.offset(2);
        make.left.equalTo(self.searchContainer).offset(10);
        make.right.equalTo(self.searchContainer).offset(-10);
        make.bottom.offset(-2);
        
    }];
}

- (CGSize)intrinsicContentSize
{
    return UILayoutFittingExpandedSize;
}

#pragma mark -- button action --
- (void)cancelBtnClick:(UIButton *)btn
{
    if (self.cancelBtnBlock)
    {
        self.cancelBtnBlock();
    }
}

@end
